home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.1 / AmigaTechniques / clipexample.c next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  7.7 KB  |  320 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*                     Copyright (c) 1985, 1988                      */
  4. /*                    Commodore-Amiga, Inc.                          */
  5. /*                    All rights reserved.                           */
  6. /*                                                                   */
  7. /*     No part of this program may be reproduced, transmitted,       */
  8. /*     transcribed, stored in retrieval system, or translated        */
  9. /*     into any language or computer language, in any form or        */
  10. /*     by any means, electronic, mechanical, magnetic, optical,      */
  11. /*     chemical, manual or otherwise, without the prior written      */
  12. /*     permission of:                                                */
  13. /*                     Commodore-Amiga, Inc.                         */
  14. /*                     16795 Lark Ave., Suite 106                    */
  15. /*                     Los Gatos, CA. 95030                          */
  16. /*                                                                   */
  17. /*********************************************************************/
  18. #include "exec/types.h"
  19. #include "exec/ports.h"
  20. #include "exec/tasks.h"
  21. #include "exec/io.h"
  22. #include "devices/clipboard.h"
  23. #include "graphics/gfx.h"
  24. #include "graphics/gfxbase.h"
  25. #include "graphics/view.h"
  26. #include "intuition/intuition.h"
  27. #include "libraries/dos.h"
  28. #include "libraries/dosextens.h"
  29.  
  30.  
  31. struct Task *FindTask();
  32. struct SatisfyMsg *GetMsg();
  33.  
  34. char buffer[80], *b, c;
  35. int rawConsole, postID;
  36.  
  37. struct IOClipReq clipboardIO = 0;
  38. struct MsgPort clipboardMsgPort = 0;
  39. struct MsgPort satisfyMsgPort = 0;
  40.  
  41. int CBOpen(unit)
  42. int unit;
  43. {
  44.     int error;
  45.  
  46.     /* open the clipboard device */
  47.     if ((error = OpenDevice("clipboard.device", unit, &clipboardIO, 0)) != 0)
  48.     return(error);
  49.  
  50.     /* Set up the message port in the I/O request */
  51.     clipboardMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  52.     clipboardMsgPort.mp_Flags = 0;
  53.     clipboardMsgPort.mp_SigBit = AllocSignal(-1);
  54.     clipboardMsgPort.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  55.     AddPort(&clipboardMsgPort);
  56.     clipboardIO.io_Message.mn_ReplyPort = &clipboardMsgPort;
  57.  
  58.     satisfyMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  59.     satisfyMsgPort.mp_Flags = 0;
  60.     satisfyMsgPort.mp_SigBit = AllocSignal(-1);
  61.     satisfyMsgPort.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  62.     AddPort(&satisfyMsgPort);
  63.  
  64.     return(0);
  65. }
  66.  
  67. CBClose()
  68. {
  69.     RemPort(&satisfyMsgPort);
  70.     FreeSignal(satisfyMsgPort.mp_SigBit);
  71.     RemPort(&clipboardMsgPort);
  72.     FreeSignal(clipboardMsgPort.mp_SigBit);
  73.     CloseDevice(&clipboardIO);
  74. }
  75.  
  76. CBCut(stream, length)
  77. char *stream;
  78. int length;
  79. {
  80.     clipboardIO.io_Command = CMD_WRITE;
  81.     clipboardIO.io_Data = (STRPTR) stream;
  82.     clipboardIO.io_Length = length;
  83.     clipboardIO.io_Offset = 0;
  84.     clipboardIO.io_ClipID = 0;
  85.     DoIO(&clipboardIO);
  86.     clipboardIO.io_Command = CMD_UPDATE;
  87.     DoIO(&clipboardIO);
  88. }
  89.  
  90. CBPaste(stream, length)
  91. char *stream;
  92. int length;
  93. {
  94.     clipboardIO.io_Command = CMD_READ;
  95.     clipboardIO.io_Data = (STRPTR) stream;
  96.     clipboardIO.io_Length = length;
  97.     clipboardIO.io_Offset = 0;
  98.     clipboardIO.io_ClipID = 0;
  99.     DoIO(&clipboardIO);
  100.     /* force end of file */
  101.     clipboardIO.io_Command = CMD_READ;
  102.     clipboardIO.io_Data = (STRPTR) 0;
  103.     clipboardIO.io_Length = 999999999;
  104.     DoIO(&clipboardIO);
  105. }
  106.  
  107.  
  108. writeLong(ldata)
  109. LONG *ldata;
  110. {
  111.     clipboardIO.io_Command = CMD_WRITE;
  112.     clipboardIO.io_Data = (STRPTR) ldata;
  113.     clipboardIO.io_Length = 4;
  114.     DoIO(&clipboardIO);
  115. }
  116.  
  117.  
  118. CBSatisfyPost(postID, string)
  119. int postID;
  120. char *string;
  121. {
  122.     int length;
  123.     char *s;
  124.  
  125.     length = 0;
  126.     s = string;
  127.     while(*s++) length++;
  128.  
  129.     clipboardIO.io_ClipID = postID;
  130.     clipboardIO.io_Offset = 0;
  131.     writeLong("FORM");            /* "FORM"    */
  132.     length += 12;
  133.     writeLong(&length);            /* #        */
  134.     writeLong("FTXT");            /* "FTXT"    */
  135.     writeLong("CHRS");            /* "CHRS"    */
  136.     length -= 12;
  137.     writeLong(&length);            /* #        */
  138.  
  139.     clipboardIO.io_Command = CMD_WRITE;
  140.     clipboardIO.io_Data = (STRPTR) string;
  141.     clipboardIO.io_Length = length;
  142.     DoIO(&clipboardIO);            /* text string    */
  143.  
  144.     clipboardIO.io_Command = CMD_UPDATE;
  145.     DoIO(&clipboardIO);
  146. }
  147.  
  148. CBCutS(string)
  149. char *string;
  150. {
  151.     CBSatisfyPost(0, string);
  152. }
  153.  
  154.  
  155. CBPasteS(string)
  156. char *string;
  157. {
  158.     int length;
  159.  
  160.     clipboardIO.io_Command = CMD_READ;
  161.     clipboardIO.io_Data = (STRPTR) 0;
  162.     clipboardIO.io_Length = 16;
  163.     clipboardIO.io_Offset = 0;
  164.     clipboardIO.io_ClipID = 0;
  165.     DoIO(&clipboardIO);
  166.  
  167.     clipboardIO.io_Command = CMD_READ;
  168.     clipboardIO.io_Data = (STRPTR) &length;
  169.     clipboardIO.io_Length = 4;
  170.     DoIO(&clipboardIO);
  171.  
  172.     clipboardIO.io_Command = CMD_READ;
  173.     clipboardIO.io_Data = (STRPTR) string;
  174.     clipboardIO.io_Length = length;
  175.     DoIO(&clipboardIO);
  176.  
  177.     string[length] = '\0';
  178.  
  179.     /* force end of file to terminate read */
  180.     clipboardIO.io_Command = CMD_READ;
  181.     clipboardIO.io_Length = 1;
  182.     clipboardIO.io_Data = (STRPTR) 0;
  183.     DoIO(&clipboardIO);
  184. }
  185.  
  186. int
  187. CBPost()
  188. {
  189.     clipboardIO.io_Command = CBD_POST;
  190.     clipboardIO.io_Data = (STRPTR) &satisfyMsgPort;
  191.     clipboardIO.io_ClipID = 0;
  192.     DoIO(&clipboardIO);
  193.     return(clipboardIO.io_ClipID);
  194. }
  195.  
  196. int
  197. CBCurrentReadID()
  198. {
  199.     clipboardIO.io_Command = CBD_CURRENTREADID;
  200.     DoIO(&clipboardIO);
  201.     return(clipboardIO.io_ClipID);
  202. }
  203.  
  204. int
  205. CBCurrentWriteID()
  206. {
  207.     clipboardIO.io_Command = CBD_CURRENTWRITEID;
  208.     DoIO(&clipboardIO);
  209.     return(clipboardIO.io_ClipID);
  210. }
  211.  
  212. BOOL
  213. CBCheckSatisfy(idVar)
  214. int *idVar;
  215. {
  216.     struct SatisfyMsg *sm;
  217.  
  218.     if (*idVar == 0)
  219.     return(TRUE);
  220.     if (*idVar < CBCurrentWriteID()) {
  221.     *idVar = 0;
  222.     return(TRUE);
  223.     }
  224.     if (sm = (struct SatisfyMsg *) GetMsg(&satisfyMsgPort)) {
  225.     if (*idVar == sm->sm_ClipID)
  226.         return(TRUE);
  227.     }
  228.     return(FALSE);
  229. }
  230.  
  231.  
  232. readS()
  233. {
  234.     b = buffer;
  235.     while (Read(rawConsole, &c, 1), (c != '\r')) {
  236.     *b++ = c;
  237.     Write(rawConsole, &c, 1);
  238.     }
  239.     *b = '\0';
  240. }
  241.  
  242.  
  243. main()
  244. {
  245.     int i;
  246.  
  247.     i = CBOpen(PRIMARY_CLIP);
  248.     if (i != 0) {
  249.     printf("CBOpen returned %ld.\n", i);
  250.     exit(20);
  251.     }
  252.  
  253.     rawConsole = Open("RAW:25/25/615/150/clipboard.device test", MODE_OLDFILE);
  254.  
  255.     Write(rawConsole, "\033[20h", 5);
  256.  
  257.     c = 0;
  258.     postID = 0;
  259.     while (c != '\033') {
  260.     while((postID) && (!WaitForChar(rawConsole, 1000000)))
  261.         if (CBCheckSatisfy(&postID)) {
  262.         if (postID) {
  263.             Write(rawConsole, "Satisfy post data\n", 18);
  264.             readS();
  265.             Write(rawConsole, "\nsatisfying \"", 13);
  266.             Write(rawConsole, buffer, strlen(buffer));
  267.             Write(rawConsole, "\"\n", 2);
  268.             CBSatisfyPost(postID, buffer);
  269.             postID = 0;
  270.         }
  271.         }
  272.     Read(rawConsole, &c, 1);
  273.     switch (c) {
  274.         case 'w':
  275.         Write(rawConsole, "Enter cut data\n", 15);
  276.         readS();
  277.         Write(rawConsole, "\ncutting \"", 11);
  278.         Write(rawConsole, buffer, strlen(buffer));
  279.         Write(rawConsole, "\"\n", 2);;
  280.         CBCutS(buffer);
  281.         break;
  282.         case 'r':
  283.         if ((postID) && (CBCurrentReadID() == postID)) {
  284.             Write(rawConsole, "previous post is still valid\n", 29);
  285.         }
  286.         else {
  287.             CBPasteS(buffer);
  288.             Write(rawConsole, "paste is \"", 10);
  289.             Write(rawConsole, buffer, strlen(buffer));
  290.             Write(rawConsole, "\"\n", 2);
  291.         }
  292.         break;
  293.         case 'p':
  294.         Write(rawConsole, "Posting post...\n", 16);
  295.         postID = CBPost();
  296.         break;
  297.         case '\033':
  298.         break;
  299.         default:
  300.         Write(rawConsole,
  301.             "w (Cut), r (Paste), p (Post), <ESC> (Quit)\n", 43);
  302.     }
  303.     }
  304.  
  305.     /* check if need to satisfy post before exiting */
  306.     if (postID) {
  307.     if (postID >= CBCurrentWriteID()) {
  308.         Write(rawConsole, "Satisfy post data before exiting\n", 18);
  309.         readS();
  310.         Write(rawConsole, "\nsatisfying \"", 13);
  311.         Write(rawConsole, buffer, strlen(buffer));
  312.         Write(rawConsole, "\"\n", 2);
  313.         CBSatisfyPost(postID, buffer);
  314.         postID = 0;
  315.     }
  316.     }
  317.     CBClose();
  318.     Close(rawConsole);
  319. }
  320.